package com.kystar.muxplayer.ui;

import android.content.res.ColorStateList;
import android.view.View;
import android.widget.Space;
import android.widget.TextView;

import com.kystar.muxplayer.Helper;
import com.kystar.muxplayer.R;

import java.util.ArrayList;

/**
 * Created by zhusheng on 2018-02-23.
 */

public class ViewList extends ArrayList<View> implements View.OnClickListener {
    View last;
    View.OnClickListener listener;

    public ViewList() {
    }

    public ViewList(View parent, boolean overwriteColor) {
        ColorStateList colors = parent.getResources().getColorStateList(R.color.settings_button_color);
        Helper.eachChild(parent, child ->{
            if (child.getTag() == null || child instanceof Space) {
                return;
            }

            TextView textView = null;
            if (child instanceof TextView) {
                textView = (TextView)child;
                add(child);
            } else {
                View text = Helper.firstChild(child, v->v instanceof TextView);
                if(text != null) {
                    add(child);
                    textView= (TextView)text;
                }
            }
            if(textView != null && overwriteColor) {
                textView.setBackgroundResource(R.drawable.settings_button_bg);
                textView.setTextColor(colors);
            }
        });
    }



    public ViewList(View parent, int buttonGroup, boolean overwriteColor) {
        this(parent.findViewById(buttonGroup), overwriteColor);
    }

    public static ViewList from(View view) {
        ViewList list = new ViewList();
        Helper.eachChild(view, list::add);
        return list;
    }

    public static ViewList from(View view, Helper.Predicate<View> predicate) {
        ViewList list = new ViewList();
        Helper.eachChild(view, child -> {
            if (predicate.apply(child)) {
                list.add(child);
            }
        });
        return list;
    }

    public static ViewList buttons(View view, boolean overwriteColor) {
        return new ViewList(view, overwriteColor);
    }

    public void addAll(View... views) {
        for (View v: views) {
            add(v);
        }
    }

    public void each(Helper.Consumer<View> consumer) {
        for (View view : this) {
            consumer.apply(view);
        }
    }

    public View first(Helper.Predicate<View> predicate) {
        for (View view : this) {
            if(predicate.apply(view)) {
                return view;
            }
        }
        return null;
    }

    public void clearSelected() {
        last = null;
        for (View view : this) {
            setSelected(view, false);
        }
    }

    public void select(View view) {
        if (last != null) {
            setSelected(last, false);
        }
        last = view;
        setSelected(last, true);
    }

    public void select(int index, boolean click) {
        if (click) {
            get(index).performClick();
        } else {
            select(get(index));
        }
    }

    public int select(String tag, boolean click) {
        for (int i = 0; i < size(); i++) {
            if (tag.equals(get(i).getTag())) {
                select(i, click);
                return i;
            }
        }
        return -1;
    }

    public View selected() {
        return last;
    }

    protected void setSelected(View view, boolean on) {
        view.setSelected(on);
    }

    public void setOnClickListener(View.OnClickListener listener) {
        this.listener = listener;
        for (View view : this) {
            view.setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        select(v);
        if (listener != null) {
            listener.onClick(v);
        }
    }
}
